﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_WA_RecordOnDutyData]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_WA_RecordOnDutyData];
GO
CREATE PROCEDURE [dbo].[sproc_WA_RecordOnDutyData]--记录上班状态
    @staffid int,       --人员id
    @dutytime datetime, --上班时间
    @dutystatus bit,    --是否迟到
    @memo nvarchar(400) --描述理由
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON

declare @lasttimestatus int;
declare @id bigint;

--检查是否迟到，如果是就向UDS_WorkAttendence_Memo插记录
IF (@dutystatus=0 ) --没有迟到
begin
    insert into dbo.UDS_WorkAttendence_Data(WorkDate, StaffID, OnDuty, OnDuty_Status) values(getdate(), @staffid, @dutytime, @dutystatus)
    return @@identity
end
else
begin
    begin transaction;

    IF((@memo is not null) and (@memo <> ''))
    BEGIN
        declare @memoid bigint
        insert into dbo.UDS_WorkAttendence_Memo(Memo) values(@memo)
        set @memoid = @@identity

        insert into dbo.UDS_WorkAttendence_Data(WorkDate,StaffID,OnDuty,OnDuty_Status,OnDuty_MemoID) values(getdate(),@staffid,@dutytime,@dutystatus,@memoid)
    END
    ELSE
    BEGIN
        insert into dbo.UDS_WorkAttendence_Data(WorkDate,StaffID,OnDuty,OnDuty_Status) values(getdate(),@staffid,@dutytime,@dutystatus)
    END
    
    commit transaction;

    return @@identity
end

END
GO